package com.arcticcoding.nxtphone;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.UUID;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

import com.arcticcoding.nxtphone.MainActivity.MovementConnection;
import com.arcticcoding.nxtphone.Network.MovementMessage;
import com.arcticcoding.nxtphone.Network.RegisterName;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;

public class RedirectListener extends Listener {
		private BTReciver 			btReciver;
        private BufferedWriter 		out;
        private static final UUID 	blueUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB") ;
        private static final String TAG = "com.arcticcoding.nxtphone.RedirectListener"; 
        
        public RedirectListener(BluetoothDevice device) {
                if(device == null)
                        throw new NullPointerException("No device");
                

                out = null;
                
                try {
                        BluetoothSocket socket;
                        socket = device.createRfcommSocketToServiceRecord(blueUUID);
                        socket.connect();
                        out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                        out.write("Hello Marvin !");
                        out.newLine();
                        out.flush();
                        Log.d(TAG, "Message send");
                        
                        btReciver = new BTReciver(new BufferedReader(new InputStreamReader(socket.getInputStream())));
                      
                        btReciver.start();
                        
                } catch (IOException e) {
                        throw new NullPointerException("WTF");
                }
        }
       
        @Override
        public void received(Connection conn, Object o) {
                MovementConnection connection = (MovementConnection) conn;
                if (o instanceof RegisterName) {
                        if (connection.name != null)
                                return;
                        String name = ((RegisterName)o).name;
                        if (name == null) return;
                        name = name.trim();
                        if (name.length() == 0) return;
                        connection.name = name;
                        MovementMessage msg = new MovementMessage();
                        msg.text = name + " connected";
                        conn.sendTCP(msg);
                        return;
                }
                if (o instanceof MovementMessage) {
                        if (connection.name == null) return;
                        MovementMessage moveMessage = (MovementMessage) o;
                        String message = moveMessage.text;
                        if (message == null) return;
                        message = message.trim();
                        if (message.length() == 0) return;
                        sendData(message);
                        return;
                }
        }
        
        public void sendData(String data) {
                try {
                        out.write(data);
                        
                        out.newLine();
                        out.flush();
                        
                        Log.d(TAG, "Data send: " + data);
                } catch(IOException e) {
                        Log.d(TAG, "Problem sending the data");
                }
        }
        
        
        public class BTReciver extends Thread implements Runnable {
        
        	BufferedReader buf;
        	
        	private boolean stop = false;
        	
        	public BTReciver(BufferedReader buf) {        		
        		if (buf == null) {
        			throw new NullPointerException("WTF");
        		}        		
        		this.buf = buf;       		
        	}        	
        	
        	public void run() {
        		
        		try {
        			
        			String line;
            		Log.d(TAG, "Start While in BTR");
            		while (((line = buf.readLine()) != null) && !stop) {
            			
            			MovementMessage msg = new MovementMessage();
                        msg.text = line;
                        Log.d(TAG, "Send Message:" + msg.text);
            			if (MainActivity.networkServer != null) {
            					MainActivity.networkServer.sendToAllTCP(msg);             					
            			}
            			
            		}
            		Log.d(TAG, "End While in BTR");
        			
        		} catch (Exception ex) {
        			Log.d(TAG, "Fehler im BTRedirect" + ex);
        		}       		
        		
        	}       	
        	
        	
        	
        }
        
}